<!--
 Copyright 2020 Google LLC
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->
{% extends 'layout.njk' %}

{% block content %}
  <h1 elementtiming="main-heading">LCP Test</h1>
  <p>
    {% if not imgDelay %}
      {% set imgDelay = 500 %}
    {% endif %}
    <img class="foo bar" elementtiming="main-image" data-target="main-image" {% if imgHidden %}hidden{% endif %} src="/test/img/square.png?delay={{ imgDelay }}">
  </p>
  <p>Text below the image</p>

  <p><a id="navigate-away" href="https://example.com">Navigate away</a></p>
  {% if prerender %}<p><a id="prerender-link" href="/test/lcp{% if queryString %}?{{ queryString | safe }}{% endif %}">Prerender link</a></p>{% endif %}

  <!-- Include a tall element to ensure scrolling is possible. -->
  <div style="height: 100vh"></div>

  <footer>Text below the full-height element.</footer>

  <script type="module">
    const {onLCP} = await __testImport('{{ modulePath }}');

    const queue = new Set();
    function addToQueue(metric) {
      queue.add(metric);
    }

    onLCP((lcp) => {
      lcp.instance = 1;

      // Log for easier manual testing.
      console.log('LCP:', lcp);

      if (self.__batchReporting) {
        console.log('Adding to queue');
        addToQueue(lcp);
      } else {
        // Test sending the metric to an analytics endpoint.
        navigator.sendBeacon(`/collect`, JSON.stringify(__toSafeObject(lcp)));
      }
    }, {
      reportAllChanges: self.__reportAllChanges,
      generateTarget: self.__generateTarget && ((el) => el.dataset.target),
    });

    if (self.__batchReporting) {
      document.addEventListener('visibilitychange', () => {
        if (document.visibilityState === 'hidden') {
          for (const lcp of queue) {
            console.log('document.addEventListener');
            navigator.sendBeacon(`/collect`, JSON.stringify(__toSafeObject(lcp)));
          }
          queue.clear();
        }
      });
    }

    if (self.__doubleCall) {
      onLCP((lcp) => {
        lcp.instance = 2;

        // Log for easier manual testing.
        console.log('LCP2:', lcp);

        // Test sending the metric to an analytics endpoint.
        navigator.sendBeacon(`/collect`, JSON.stringify(__toSafeObject(lcp)));
      }, {
        reportAllChanges: self.__reportAllChanges2,
        generateTarget: self.__generateTarget2 && ((el) => el.dataset.target),
      });
    }
  </script>
  {% if prerender %}
  <script type="speculationrules">
  {
    "prerender": [
      {
        "urls": ["/test/lcp{% if queryString %}?{{ queryString | safe }}{% endif %}"]
      }
    ]
  }
  </script>
{% endif %}
{% endblock %}
